﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace h1
{
    public partial class Form_register : Form
    {
        public Form_register()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                errorProvider1.SetError(textBox1, "请输入用户名");
            }
            else if(textBox2.Text == "")
            {
                errorProvider1.SetError(textBox2, "请输入密码");
            }
            else if(textBox3.Text == "")
            {
                errorProvider1.SetError(textBox3, "请再次输入密码");
            }
            else if (textBox3.Text!=textBox2.Text)
            {
                errorProvider1.SetError(textBox3, "密码不一致，请重新输入");
            }
            else
            {
                errorProvider1.Clear();
                DataSet ds_users = MySqlHelper.getDataSet(MySqlHelper.Conn, "SELECT * FROM users");
                string id = "0000000000";
                foreach(DataRow dr in ds_users.Tables[0].Rows)
                {
                    if (dr["userName"].ToString() == textBox1.Text)
                    {
                        Form_message fm = new Form_message("注册失败！","错误描述：用户名已存在！请重新输入！");
                        fm.ShowDialog();
                        return;
                    }
                    if (dr["userID"].ToString().CompareTo(id) > 0)
                    {
                        id = dr["userID"].ToString();
                    }
                }
                DataRow user_new = ds_users.Tables[0].NewRow();
                id = (Convert.ToUInt32(id)+1).ToString().PadLeft(10,'0');
                user_new["userID"] = id;
                user_new["userName"] = textBox1.Text;
                user_new["userPassword"] = textBox2.Text;
                ds_users.Tables[0].Rows.Add(user_new);
                MySqlHelper.updateDataSet(MySqlHelper.Conn, "SELECT * FROM users", ds_users);
                Form_message fm2 = new Form_message("注册成功！", $"用户ID为：{id}\n用户名为：{textBox1.Text}\n【提示】：为了方便找回密码，请及时设置密保问题！",id,0);
                fm2.ShowDialog();
                this.Close();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (button5.ImageIndex == 0)
            {
                button5.ImageIndex = 1;
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {
                button5.ImageIndex = 0;
                textBox2.UseSystemPasswordChar = true;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.ImageIndex == 0)
            {
                button2.ImageIndex = 1;
                textBox3.UseSystemPasswordChar = false;
            }
            else
            {
                button2.ImageIndex = 0;
                textBox3.UseSystemPasswordChar = true;
            }
        }

        private void Form_register_Load(object sender, EventArgs e)
        {

        }
    }
}
